//
//  LJMRankSubRoot_Soner_Son_CollectionViewCell.m
//  wind
//
//  Created by 李金明 on 16/1/12.
//  Copyright © 2016年 李金明. All rights reserved.
//

#import "LJMRankSubRoot_Soner_Son_CollectionViewCell.h"
#import "LJMRankSubRoot_Soner_Son_TableViewCell.h"
#import "LJMListen_Model.h"
#define CELLHEIGHT 60//CELL的高度
@interface LJMRankSubRoot_Soner_Son_CollectionViewCell () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) NSMutableArray *dataArr;

@end


@implementation LJMRankSubRoot_Soner_Son_CollectionViewCell
- (void)setSinger_id:(NSString *)singer_id
{
    if (_singer_id != singer_id) {
        [_singer_id release];
        _singer_id = [singer_id retain];
    }

    //获取网路数据
    [self GetSonData];
    //添加tableView
    [self addTablView];
}
- (void)dealloc
{
    [_dataArr release];
    [_tableV release];
    [_singer_id release];
    [super dealloc];
    
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}
- (void)creatView
{
    self.backgroundColor = [UIColor whiteColor];

}

#pragma 添加tableView
- (void)addTablView
{
    self.tableV = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableV];
    [_tableV release];
}
#pragma tableView的协议方法
#pragma 数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.dataArr.count;
    
}
#pragma cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *str = @"cell";
    LJMRankSubRoot_Soner_Son_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (nil == cell) {
        cell = [[LJMRankSubRoot_Soner_Son_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    LJMListen_Model *model = [self.dataArr objectAtIndex:indexPath.row];
    cell.numLab.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row];
    cell.titleLab.text = model.name;
    cell.sonerLab.text = model.singerName;
    cell.sonerStr = model.singerName;

    return cell;
    
}
#pragma 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CELLHEIGHT;
}
#pragma 点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //传递过去数组和当前点击的界面
    [self.delegate changeSonData:self.dataArr NSInter:indexPath.row];
}
#pragma 跟着tabelView的随动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.tableV == scrollView) {
        CGFloat height = scrollView.contentOffset.y;
        if (height < 0) {
    [self.sontableDelegate tableViewCGFloat:height];
       
        } else {
            [self.sontableDelegate tableViewCGFloat:height];
        
        }
        

        
    }
}
#pragma 获取单曲的数据
- (void)GetSonData
{
    NSString *upstr = @"http://api.dongting.com/song/singer/";
    NSString *downstr = @"/songs?page=1&size=50&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=38.88281&longtitude=121.5392 HTTP/1.1";
    NSString *urlStr = [NSString stringWithFormat:@"%@%@%@", upstr, self.singer_id, downstr];
    
[LJMNetworkTool getNetWithURL:urlStr body:nil headFile:nil responseStyle:LJMResponseJSON success:^(NSURLSessionDataTask *task, id responseObject) {
    if (nil != responseObject) {

        NSArray *arr = [responseObject objectForKey:@"data"];
        self.dataArr = [NSMutableArray array];
        for (NSMutableDictionary *tempDic in arr) {
            NSArray *arr = [tempDic objectForKey:@"auditionList"];
            //如果有歌曲则放入数组
            if (0 != arr.count) {
                
            LJMListen_Model *model = [[LJMListen_Model alloc] init];
            //装入播放的音乐
            model.auditionListArr = [NSMutableArray arrayWithArray:[tempDic objectForKey:@"auditionList"]];
            

            [model initWithDic:tempDic];
            //如果有无损音乐
            
            
            if (nil != [tempDic objectForKey:@"llList"] && ![[tempDic objectForKey:@"llList"] isEqual:[NSNull null]]) {
                model.llList = [[[tempDic objectForKey:@"llList"] objectAtIndex:0] objectForKey:@"url"];
            }
            
            
            //装入作者的信息
            model.singersArr = [NSMutableArray arrayWithArray:[tempDic objectForKey:@"singers"]];
            
            [self.dataArr addObject:model];
            [model release];
            }
        }
    }
    
#pragma 缓存数据到本地
    NSString *path = [NSString stringWithFormat:@"%ld.plist", (long)[urlStr hash]];
    // 存储的沙盒路径
    NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // 归档
    [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
    
    [self.tableV reloadData];
    
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    
    // 缓存的文件夹
    NSString *path = [NSString stringWithFormat:@"%ld.plist", (long)[urlStr hash]];
    NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // 反归档
    id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
    
    if (result) {
        NSArray *arr = [result objectForKey:@"data"];
        self.dataArr = [NSMutableArray array];
        for (NSMutableDictionary *tempDic in arr) {
            NSArray *arr = [tempDic objectForKey:@"auditionList"];
            //如果有歌曲则放入数组
            if (0 != arr.count) {
                
                LJMListen_Model *model = [[LJMListen_Model alloc] init];
                //装入播放的音乐
                model.auditionListArr = [NSMutableArray arrayWithArray:[tempDic objectForKey:@"auditionList"]];
                
                
                [model initWithDic:tempDic];
                //如果有无损音乐
                
                
                if (nil != [tempDic objectForKey:@"llList"] && ![[tempDic objectForKey:@"llList"] isEqual:[NSNull null]]) {
                    model.llList = [[[tempDic objectForKey:@"llList"] objectAtIndex:0] objectForKey:@"url"];
                }
                
                
                //装入作者的信息
                model.singersArr = [NSMutableArray arrayWithArray:[tempDic objectForKey:@"singers"]];
                
                [self.dataArr addObject:model];
                [model release];
            }
        }

        [self.tableV reloadData];

    } else {
        NSLog(@"error = %@", error);
    }

    
}];
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
